function setContent(contentPage){
    $("#content").load(contentPage);
}

function getURLparam(pName){
    var sPageURL = window.location.search.substring(1);
    var URLVariables = sPageURL.split('&');
    for(var i=0; i<URLVariables.length; i++){
        var paramName = URLVariables[i].split("=");
        if(paramName[0] === pName){
            return paramName[1];
        }
    }
}

function showInfobox(identity, info){
    var id = identity.charAt(0) === "#" ? identity : "#" + identity;
    var pos = $(id).offset();
    var top = pos.top - 190;
    var left = pos.left + $(id).width();
    
    $("#infobox").remove();
    
    $("body").append("<div id='infobox' class='infobox' style='top: "+ top +"px; left: " + left + "px; '>" + info + "</div>");
    setTimeout(function(){
       $("#infobox").remove();
    }, 1500);
}

function setRatingStars(rating){
    var tab = rating.split(".");
    var goldenStars = Number(tab[0]);
    var decimal = Number(tab[1]);
    for(var i=1; i<(goldenStars+1); i++){
        $("#rating-star-" + i).attr("class","gold-star");
    }
    if(decimal > 25 && decimal < 75){
        $("#rating-star-" + (goldenStars+1)).attr("class","half-star");
    } else if(decimal >= 75){
         $("#rating-star-" + (goldenStars+1)).attr("class","gold-star");
    }
    
}

function showLogInDialog(){
    $('#outOfLogInDialog').fadeToggle("fast", function(){
        document.body.addEventListener('click', closeLogInDialog, false);
        $("#logInEmail").focus();
    });
};

function closeLogInDialog(e){
    if(e.target.id === 'outOfLogInDialog'){
        document.body.removeEventListener('click', closeLogInDialog, false);
        $('#outOfLogInDialog').fadeToggle("fast");
        if($('#sendPasswordTable').is(":visible")){
            var height =  $('#logInDialog').height();
            var minus = height - 50;
            $('#sendPasswordTable').hide("fast");
            $('#logInDialog').animate({"height" : minus}, 10);
        }
    }
}

function cancelLogInBtnClicked(){
    document.body.removeEventListener('click', closeLogInDialog, false);
    $('#outOfLogInDialog').fadeToggle("fast");
    if($('#sendPasswordTable').is(":visible")){
        var height =  $('#logInDialog').height();
        var minus = height - 50;
        $('#sendPasswordTable').hide("fast");
        $('#logInDialog').animate({"height" : minus}, 10);
     }
}

function showSendNewPassword(){
    var height =  $('#logInDialog').height();
    var plus = height + 50;
    var minus = height - 50;
    if($('#sendPasswordTable').is(":hidden")){
        $('#logInDialog').animate({"height" : plus}, 200);
        $('#sendPasswordTable').slideDown("fast");
    } else{
        $('#sendPasswordTable').slideUp("fast");
        $('#logInDialog').animate({"height" : minus}, 200);
         
    }
   
}

function getClientInfo(){
    var dfd = new $.Deferred();
    jQuery.ajax({ 
        url: '//freegeoip.net/json/', 
        type: 'POST', 
        dataType: 'jsonp',
        success: function(location) {
            // example where I update content on the page.
            /*jQuery('#city').html(location.city);
            jQuery('#region-code').html(location.region_code);
            jQuery('#region-name').html(location.region_name);
            jQuery('#areacode').html(location.areacode);
            jQuery('#ip').html(location.ip);
            jQuery('#zipcode').html(location.zipcode);
            jQuery('#longitude').html(location.longitude);
            jQuery('#latitude').html(location.latitude);
            jQuery('#country-name').html(location.country_name);
            jQuery('#country-code').html(location.country_code);*/
            dfd.resolve(location);
        }
    });
    return dfd.promise();
}

function deleteRow(btnId){
    $(btnId).closest("tr").remove();
}

function addRowToGrid(gridId){
    var largestIndex = 0;
    $("#" + gridId + " > tbody > tr").each(function(i, tr){
        if(Number(tr.id[tr.id.length-1] > largestIndex)){
            largestIndex = Number(tr.id[tr.id.length-1]);
        }
    });
    var rowCounter = largestIndex + 1;
    switch(gridId){
        case("new-bar-grid"):
            var html = "<tr id='row-" + rowCounter + "'>";
            html += "<td id='name-"+rowCounter+"'>" + $("#name").val() + "</td>";
            html += "<td id='country-"+rowCounter+"'>" + $("#country").val("Norway") + "</td>";
            html += "<td id='place-"+rowCounter+"'>" + $("#place").val() + "</td>";
            html += "<td id='postal_no-"+rowCounter+"'>" + $("#postal_no").val() + "</td>";
            html += "<td id='address-"+rowCounter+"'>" + $("#address").val() + "</td>";
            html += "<td id='cover_charge-"+rowCounter+"'>" + $("#cover_charge").val() + "</td>";
            html += "<td id='beer_price-"+rowCounter+"'>" + $("#beer_price").val() + "</td>";
            html += "<td id='description-"+rowCounter+"'>" + $("#description").val() + "</td>";
            html += "<td id='age_limit-"+rowCounter+"'>" + $("#age_limit").val() + "</td>";
            html += "<td id='user_id-"+rowCounter+"'>" + $("#user_id").val() + "</td>";
            html += "<td id='status_id-"+rowCounter+"'>" + $("#status_id").val() + "</td>";
            html += "<td>" + "<input class='delete-row-btn btn' type='button' onclick='deleteRow(this);' title='Delete row'>" + "</td>";
            html += "</tr>";
            break;
        case("new-user-grid"):
            html += "<tr id='row-" + rowCounter + "'>";
            html += "<td id='username-"+rowCounter+"'>" + $("#username").val() + "</td>";
            html += "<td id='email-"+rowCounter+"'>" + $("#email").val() + "</td>";
            html += "<td id='firstname-"+rowCounter+"'>" + $("#firstname").val() + "</td>";
            html += "<td id='lastname-"+rowCounter+"'>" + $("#lastname").val() + "</td>";
            html += "<td id='password-"+rowCounter+"'>" + $("#password").val() + "</td>";
            html += "<td id='accounttype-"+rowCounter+"'>" + $("#account_type").val() + "</td>";
            html += "<td>" + "<input class='delete-row-btn button' type='button' onclick='deleteRow(this)' title='Delete row'>" + "</td>";
            html += "</tr>";
            break;
        default:
            break;
    }
    $("#" + gridId).append(html);
     reset("#" + gridId);
}

function reset(gridId){
    switch(gridId){
        case ("new-bar-grid"):
            $("#name").val("");
            $("#country").val("Norway");
            $("#city").val("");
            $("#postal-no").val("");
            $("#address").val("");
            $("#cc").val("0");
            $("#beer-price").val("");
            $("#description").val("");
            $("#age-limit").val("");
            $("#user_id").val("");
            break;
        case ("new-user-grid"):
            $("#username").val("");
            $("#email").val("");
            $("#firstname").val("");
            $("#lastname").val("");
            $("#password").val("");
            $("#account_type").val("");
            break;
        default:
            break;
    }
}

function validate(){
    var isValid = true;
    if(!$("#name").val()){
        isValid = false;
    } else if(!$("#country").val()){
        isValid = false;
    } else if(!$("#city").val()){
        isValid = false;
    } else if(!$("#postal-no").val()){
        isValid = false;
    } else if(!$("#address").val()){
        isValid = false;
    } else if(!$("#beer-price").val()){
        isValid = false;
    } else if(!$("#description").val()){
        isValid = false;
    } else if(!$("#age-limit").val()){
        isValid = false;
    }
    return isValid;
}

function logout(){
    location.href='../admin/logOut.php'
}

$(document).ready(function() {
    $('#headerMenu > li').bind('mouseover', openSubMenu);
    $('#headerMenu > li').bind('mouseout', closeSubMenu);
    $("#cancelLoginBtn").click(function(){
        cancelLogInBtnClicked();
    });
    
    function openSubMenu() {
	$(this).find('ul').css('visibility', 'visible');	
    };
    
    function closeSubMenu() {
            $(this).find('ul').css('visibility', 'hidden');	
    };
});